package platform

import (
	"net/http"
	"time"

	"gitee.com/wangshh_123/cross-pay-go/src/platform/providers/juswaypay"
	"gitee.com/wangshh_123/cross-pay-go/src/platform/providers/juswaypay/models"
	"gitee.com/wangshh_123/cross-pay-go/src/platform/providers/juswaypay/services"
	"gitee.com/wangshh_123/cross-pay-go/src/platform/providers/juswaypay/validator"
	"gitee.com/wangshh_123/cross-pay-go/src/utils/common"

	"github.com/gin-gonic/gin"
)

// JuswayPayController handles JuswayPay related requests
type JuswayPayController struct {
	client *juswaypay.JuswayPayClient
}

// NewJuswayPayController creates a new JuswayPayController instance
func NewJuswayPayController(client *juswaypay.JuswayPayClient) *JuswayPayController {
	return &JuswayPayController{
		client: client,
	}
}

// GetUserInfo 获取用户信息
func (c *JuswayPayController) GetUserInfo(ctx *gin.Context) {
	userInfo, err := services.GetUserInfo(ctx.Request.Context(), c.client)
	if err != nil {
		common.Error(ctx, http.StatusInternalServerError, "get_user_info_failed", err)
		return
	}

	common.Success(ctx, userInfo.Data)
}

// GetMemberCenter gets the member center information
func (c *JuswayPayController) GetMemberCenter(ctx *gin.Context) {
	memberCenter, err := services.GetMemberCenter(ctx.Request.Context(), c.client)
	if err != nil {
		common.Error(ctx, http.StatusInternalServerError, "get_member_center_failed", err)
		return
	}

	common.Success(ctx, memberCenter.Data)
}

// GetCardScenario gets the card scenario list
func (c *JuswayPayController) GetCardScenario(ctx *gin.Context) {
	scenarios, err := services.GetCardScenario(ctx.Request.Context(), c.client)
	if err != nil {
		common.Error(ctx, http.StatusInternalServerError, "get_card_scenario_failed", err)
		return
	}

	common.Success(ctx, scenarios.Data)
}

// GetCardBinRange gets the card bin range list
func (c *JuswayPayController) GetCardBinRange(ctx *gin.Context) {
	binRanges, err := services.GetCardBinRange(ctx.Request.Context(), c.client)
	if err != nil {
		common.Error(ctx, http.StatusInternalServerError, "get_card_bin_range_failed", err)
		return
	}

	common.Success(ctx, binRanges.Data)
}

// GetChannelList gets the channel list
func (c *JuswayPayController) GetChannelList(ctx *gin.Context) {
	channels, err := services.GetChannelList(ctx.Request.Context(), c.client)
	if err != nil {
		common.Error(ctx, http.StatusInternalServerError, "get_channel_list_failed", err)
		return
	}

	common.Success(ctx, channels.Data)
}

// CreateCardHolder 创建持卡人
func (c *JuswayPayController) CreateCardHolder(ctx *gin.Context) {
	var req models.CardHolderRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		common.Error(ctx, http.StatusBadRequest, "invalid_request_body", err)
		return
	}

	// 参数校验
	if err := validator.ValidateCardHolderRequest(&req); err != nil {
		common.Error(ctx, http.StatusBadRequest, "invalid_request_params", err)
		return
	}

	// 调用服务层创建持卡人
	cardHolder, err := services.CreateCardHolder(ctx.Request.Context(), c.client, &req)
	if err != nil {
		common.Error(ctx, http.StatusInternalServerError, "create_card_holder_failed", err)
		return
	}

	common.Success(ctx, cardHolder.Data)
}

// ApplyCard 开卡申请
func (c *JuswayPayController) ApplyCard(ctx *gin.Context) {
	var req models.CardApplyRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		common.Error(ctx, http.StatusBadRequest, "invalid_request_body", err)
		return
	}

	// 设置开始时间为当前日期
	now := time.Now()
	req.StartDate = now.Format("2006-01-02")
	// 设置结束时间为一年后的今天
	req.EndDate = now.AddDate(1, 0, 0).Format("2006-01-02")

	// 参数校验
	if err := validator.ValidateCardApplyRequest(&req); err != nil {
		common.Error(ctx, http.StatusBadRequest, "invalid_request_params", err)
		return
	}

	// 调用服务层开卡申请
	cardApply, err := services.ApplyCard(ctx.Request.Context(), c.client, &req)
	if err != nil {
		common.Error(ctx, http.StatusInternalServerError, "apply_card_failed", err)
		return
	}

	common.Success(ctx, cardApply.Data)
}

// RechargeCard 充值
func (c *JuswayPayController) RechargeCard(ctx *gin.Context) {
	var req models.CardRechargeRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		common.Error(ctx, http.StatusBadRequest, "invalid_request_body", err)
		return
	}

	// 从配置中获取二级密码
	req.TwoPassword = c.client.GetConfig().TwoPassword

	// 参数校验
	if err := validator.ValidateCardRechargeRequest(&req); err != nil {
		common.Error(ctx, http.StatusBadRequest, "invalid_request_params", err)
		return
	}

	// 调用服务层充值
	recharge, err := services.RechargeCard(ctx.Request.Context(), c.client, &req)
	if err != nil {
		common.Error(ctx, http.StatusInternalServerError, "recharge_card_failed", err)
		return
	}

	common.Success(ctx, recharge.Data)
}

// RefundCard 退款
func (c *JuswayPayController) RefundCard(ctx *gin.Context) {
	var req models.CardRefundRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		common.Error(ctx, http.StatusBadRequest, "invalid_request_body", err)
		return
	}

	// 从配置中获取二级密码
	req.TwoPassword = c.client.GetConfig().TwoPassword

	// 参数校验
	if req.CardID <= 0 {
		common.Error(ctx, http.StatusBadRequest, "invalid_card_id", nil)
		return
	}
	if req.RefundAmount <= 0 {
		common.Error(ctx, http.StatusBadRequest, "invalid_refund_amount", nil)
		return
	}

	// 调用服务层退款
	refund, err := services.RefundCard(ctx.Request.Context(), c.client, &req)
	if err != nil {
		common.Error(ctx, http.StatusInternalServerError, "refund_card_failed", err)
		return
	}

	common.Success(ctx, refund.Data)
}
